﻿using System.ComponentModel;
using System.Runtime.Serialization;

namespace SharpGL.SceneGraph
{
    /// <summary>
    /// An index into a set of arrays.
    /// </summary>
    [TypeConverter(typeof(System.ComponentModel.ExpandableObjectConverter))]
    [DataContract(/*IsReference = true, */Name = "Index", Namespace = "SharpGL.SceneGraph")]
    public class Index
    {
        #region Private Fields

        /// <summary>
        /// This is the point in the polygon point array that the index refers to.
        /// </summary>
        [DataMember()]
        private int vertex = -1;

        /// <summary>
        /// This is the material coord in the polygon UV array that the index refers to.
        /// </summary>
        [DataMember()]
        private int uv = -1;

        /// <summary>
        /// This is the index into the normal array for this point. a value of -1 will
        /// generate a normal on the fly.
        /// </summary>
        [DataMember()]
        private int normal = -1;

        #endregion Private Fields

        #region Constructors

        /// <summary>
        /// Initializes a new instance of the <see cref="Index"/> class.
        /// </summary>
        public Index()
        {
        }
        
        public Index(Index index)
        {
            this.vertex = index.vertex;
            this.uv = index.uv;
            this.normal = index.normal;
        }
        
        public Index(int vertex, int uv = -1, int normal = -1)
        {
            this.vertex = vertex;
            this.uv = uv;
            this.normal = normal;
        }

        #endregion Constructors

        #region Public Properties

        /// <summary>
        /// Gets or sets the point.
        /// </summary>
        /// <value>
        /// The point.
        /// </value>
        [Description("The point index from the Polygons Point3D Array."), Category("Index")]
        public int Vertex
        {
            get { return this.vertex; }
            set
            {
                if (value < 0)
                    this.vertex = -1;
                else
                    this.vertex = value;
            }
        }

        /// <summary>
        /// Gets or sets the normal.
        /// </summary>
        /// <value>
        /// The normal.
        /// </value>
        [Description("The normal index, -1 means a normal will be generated on the fly."), Category("Index")]
        public int Normal
        {
            get { return this.normal; }
            set
            {
                if (value < 0)
                    this.normal = -1;
                else
                    this.normal = value;
            }
        }

        /// <summary>
        /// Gets or sets the UV.
        /// </summary>
        /// <value>
        /// The UV.
        /// </value>
        [Description("The UV index from the polygons UV Array."), Category("Index")]
        public int UV
        {
            get { return this.uv; }
            set
            {
                if (value < 0)
                    this.uv = -1;
                else
                    this.uv = value;
            }
        }

        #endregion Public Properties
    }
}
